package com.web.kongjian.ao.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.Message;
import com.base.kongjian.service.MessageService;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.web.kongjian.ao.MessageAo;
import com.web.user.member.utils.CookieUtils;

@Service("messageAo")
public class MessageAoImpl implements MessageAo
{
	@Autowired
	private MessageService messageService;
	@Autowired
	private MemberService memberService;
	
	@Override
	public void leaveMessage(Message message,HttpServletRequest request)
	{
		message.setStatus (1);
		message.setType (1);
		
		long sendId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		message.setSend_MId (sendId);
		
		messageService.sendMessage (message);
	}

	@Override
	public void delete(long messageId, HttpServletRequest request)
	{
		String str = CookieUtils.getCookieValue (request ,"member_id");
		long memberId = 0L;
		
		if(!StringUtils.isTrimEmpty (str))
		{
			memberId = Long.parseLong (str);
		}
		
		Message message  = messageService.getMessageById (messageId);
		
		if(ObjectUtils.notNull (message)&&memberId==message.getMemberId ())
		{
			messageService.deleteMessageById (messageId);
		}
	}

	@Override
	public void reply(HttpServletRequest request, long memberId, String content)
	{
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long sendM_Id = 0L;
		if(!StringUtils.isTrimEmpty (str))
		{
			sendM_Id = Long.parseLong (str);
		}
		
		Message me = new Message ();
		
		me.setContent (content);
		me.setMemberId (memberId);
		me.setSend_MId (sendM_Id);
		me.setStatus (1);//δ��
		me.setType (1);//�ռ�����
		
		messageService.sendMessage (me);
	}

	@Override
	public void refuse(HttpServletRequest request, long memberId)
	{
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long sendM_Id = 0L;
		if(!StringUtils.isTrimEmpty (str))
		{
			sendM_Id = Long.parseLong (str);
		}
		
		Message me = new Message ();
		
		Member m =	memberService.getMemberById (sendM_Id);
		
		String nickName = "";
		if(ObjectUtils.notNull (m))
		{
			nickName =	m.getNickName ();
		}
		
		me.setContent (nickName+"�ܾ�������������!");
		me.setMemberId (memberId);
		me.setSend_MId (sendM_Id);
		me.setStatus (1);//δ��
		me.setType (4);//�ռ�����
		messageService.sendMessage (me);
	}
}
